
<script src="components/com_frmessage/public/uploadlib/jquery.min.js" type="text/javascript"></script>
<!-- <script src="components/com_frmessage/public/uploadlib/jquery.uploadify.min.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="components/com_frmessage/public/uploadlib/uploadify.css"/>
<style type="text/css">
    .uploadify-button {
        background-color: transparent;
        border: none;
        padding: 0;
    }
    .uploadify:hover .uploadify-button {
        background-color: transparent;
    }
</style> -->
<!-- LEFT -->
<?php ini_set('memory_limit', '256M'); ?>
 <?php echo $this->loadTemplate('left','default') ?>
 <!-- END LEFT -->
 <!-- CONTENT -->
 <form action="index.php?top=page&option=com_frmessage" method="post" name="adminForm" id="adminForm" enctype="multipart/form-data">

     <div id="jm-content">
        <div id="">
         <h3 class="jm-title">メッセージ編集</h3>
         <div id="container">
            <table style="width: 100%;">
                    <tr>
                        <td width="50px">宛先</td>
                        <td width="332px">
                            <?php

                                if(substr($this->recipient, -1) != ';' && $this->recipient) {
                                    $this->recipient .= ';';
                                    $this->recipient_hidden .= ';';
                                }
                                if(strpos($this->recipient, '(') && strpos($this->recipient, ')') ){
                                    //$this->
                                }
                            ?>
                            <textarea id="OCBA_CODE_LIST_SHOW" name="jform[OCBA_CODE_LIST_SHOW]" rows="1" readonly="true" style="height: 50px; " class="required input-big"><?php echo trim($this->recipient) ?></textarea>
                            <textarea id="OCBA_CODE_LIST" name="jform[OCBA_CODE_LIST]" rows="1" style="height: 50px; " class="required input-big hidden"><?php echo $this->recipient_hidden ?></textarea>
                        </td>
                        <td id=""><button id="btn_select_recipient" class="button-edit">配信先選択</button></td>

                    </tr>
            </table>
            <table style="width: 100%;">
                    <tr>
                        <td width="50px">件名</td>
                        <td colspan="6">
                            <input name="jform[TITLE]" value="<?php echo $this->TITLE?>" type="text" class="input-big" id="TITLE"/>
                        </td>
                    </tr>
                    <tr><td width="50px"></td>
                        <td>
                            <button class="button-edit toolbar" id="save">送信</button>
                            <button class="button-edit toolbar" id="savetemp">下書き保存</button>
                            <button href="index.php?top=page&option=com_frmessage&cate=inbox">戻る</button>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="7">メッセージ本文</td>
                    </tr>
                    <tr>
                        <td colspan="7">
                            <textarea name="jform[BODY]" rows="15" cols="72" ><?php echo $this->BODY?></textarea>
                        </td>
                    </tr>

               </table>
               <div id="attachment">
                    <input type="file" name="Filedata[]" class="att"/>
                    <input type="file" name="Filedata[]" class="att"/>
                    <input type="file" name="Filedata[]" class="att"/>
                    <input type="file" name="Filedata[]" class="att"/>
                    <input type="file" name="Filedata[]" class="att"/>
                    <input type="file" name="Filedata[]" class="att"/>
                    <br/> <br/>
                    <label for="unzip">
                        <input type='checkbox' name='jform[unzip]' id='unzip' value='1'  />
                        <strong>ZIPファイルを展開</strong>
                    </label><br/><br/>
                    <input type='submit' name='upload_att' value='Upload' id='btn_upload' /> 
               </div>

                <!-- <?php if( JRequest::getVar('task') != 'forward' ){?>
                        <div id="upload">
                            <a href="javascript:$('#file_upload').uploadify('upload','*')" class="uploadfile"></a>
                            <a href="#" id="file_upload"></a>
                        </div>
                    <?php } ?> -->

               <table id="tb_blue" style="width: 100%;">
    	       <thead>
                    <th></th>
                    <th>添付ファイル名</th>
                    <?php echo JRequest::getVar('task') != 'forward' ? "<th>送信先</th>" : null;?>
                    <th>サイズ</th>
                    <th>ステータス</th>

                    <?php echo JRequest::getVar('task') != 'forward' ? "<th>作業</th>" : null;?>
                </thead>
        		<tbody id="content-upload">

                <?php
                if($this->att){
                    $i = 1;

                    $img = "<img src='components/com_frmessage/public/images/checked-old.png'/>";
                    foreach($this->att as $k=>$rec){
                        $encode_k = sha1($k);
                        if(!file_exists(PATH_UPLOAD."/".$encode_k)){
                            continue;
                        }

                        $size = filesize(PATH_UPLOAD."/$encode_k");
                        $size = ceil($size/1024);
                        $link = "<a href='".PATH_DOWNLOAD.urlencode($k)."'>$k</a>";
                        echo "<tr>
                            <td align='center'>$i</td>
                            <td>$link</td>";

                        echo JRequest::getVar('task') != 'forward' ? "<td id='re_$k' class='$k' width='200'>$rec</td>" : null;
                        echo "<td>$size kb</td>
                            <td align='center'>$img</td>";

                        echo JRequest::getVar('task') != 'forward' ? "<td><a href='#'class='del' rel='$k' file='no'>削除</a></td>" : null;
                        echo "</tr>";
                        $i++;
                    }
                }?>
                </tbody>
         </table>
         </div>
        <div id="hidden">
            <textarea name='attachment' id='attachment_value' class="hidden"><?php echo json_encode($this->att)?></textarea>
            <input type="hidden" name="task" id="task" value=""/>
            <input type="hidden" name="type" id="type" value="<?php echo JRequest::getVar('task')?>"/>
            <input type="hidden" name="cate" id="cate" value="<?php echo JRequest::getVar('cate')?>"/>
            <input type="hidden" name="cid" id="cid" value="<?php echo JRequest::getVar('cid')?>"/>
        </div>
       </div>
     </div>
 <!-- END CONTENT -->
</form>

<script type="text/javascript">

// <?php $timestamp = time();?>
// var stt = $('#content-upload tr').length + 1;
// var fileCSVName = '';
// $(function() {
// 	$('#file_upload').uploadify({
// 		'formData'     : {
// 			'timestamp' : '<?php echo $timestamp;?>',
// 			'token'     : '<?php echo md5('unique_salt' . $timestamp);?>'
// 		},
//         'removeTimeout' : 1,
//         'fileSizeLimit' : '26MB',
//         'buttonText' : '',
//         'successTimeout' : 300,
//         'auto'     : false,
//         'buttonImage' : 'components/com_frmessage/public/images/btnchon.png',
// 		'swf'      : 'components/com_frmessage/public/uploadlib/uploadify.swf',
// 		'uploader' : 'components/com_frmessage/public/uploadlib/uploadify.php',
//         'fileTypeDesc' : 'All file',
//         'fileTypeExts' : '*',

//         'onUploadSuccess' : function(file, data, response) {
//             //alert(data);
//             if(file.name == 'recipients.csv') {
//                 fileCSVName = data;
//                 return;
//             }

//             $('#content-upload').append(
//                         "<tr>\
//                             <td align='center'>"+stt+"</td>\
//                             <td><a href='<?php echo PATH_DOWNLOAD ?>"+encodeURIComponent(data)+"' target='_blank'>"+data+"</a></td>\
//                             <td id='re_"+file.name+"' class='"+data+"'></td>\
//                             <td>"+bytesToSize(file.size)+"</td>\
//                             <td align='center'><img src='components/com_frmessage/public/images/checked.png'/></td>\
//                             <td><a href='#'class='del' rel='"+data+"'>削除</a></td>\
//                         </tr>");
//             stt++;
//         },

//         'onQueueComplete' : function(queueData) {
//             //if(fileCSVName == '') return;
//             getRecipient();// giu lai gia tri danhsach ng nhan json
//             $.post("components/com_frmessage/public/uploadlib/ajaxReadCSV.php",{ name : fileCSVName })
//             .done(function(data){
//                 //console.log(data);
//                 var list_re = jQuery.parseJSON(data);

//                 //console.log(list_re);
//                 for(file in list_re){ // for file name 1.pdf

//                     // td file name, khong dung jquery vi ten file co dang 1.pdf, bi hieu lam voi class selector
//                     var td_rec = document.getElementById('re_'+file); //jq('#re_'+file);

//                     //alert('re_'+file);
//                     if(!td_rec){
//                         continue;
//                     }
//                     // xoa recipient cu, nhap moi hoan toan
//                     td_rec.innerHTML = '';

//                     for(recipient in list_re[file]){ // for recipient array(0=>'U_1001', 1=>'G_2')
//                         var name_rec = list_re[file][recipient];

//                         if(td_rec.innerHTML){ // 2 ng nhan tro len
//                             td_rec.innerHTML += ','+name_rec;
//                         }else{ // 1 ng nhan duy nhat
//                             td_rec.innerHTML = name_rec;
//                         }

//                         // gan cac input hidden


//                     }// end for recipient
//                 } // end for file name
//                 getRecipient();
//                 //jq('#renamed_value').val(JSON.stringify(renamed)); // danh sach cac ten file da duoc rename

//             }); // end done post ajax

//         }

// 	});
// });

checkDisableButton();


//button id upload
$('#btn_upload').click(function(){
    $('#task').val('upload');
})

// lay danh sach ten file va nguoi nhan tuong ung
function getRecipient(){
    var data = {};
    $('td[id^="re_"]').each(function(key){
        var value = $(this).html();
        //if(value){
            data[$(this).attr('class')] = value;
        //}
    });


    $.post("components/com_frmessage/public/uploadlib/json.php",{ name : data })
        .done(function(dt){
            $('#attachment_value').val(dt);
            //alert(dt);
        });
}

$('#tb_blue tbody').on('click', '.del', function(){
    $this = jq(this);
    if(confirm('選択したデータを削除します。よろしいですか？')){
        // xoa dong hien tai trong table
        $this.parent().parent().remove();

        //set lai danh sach attachment
        jq('#attachment_value').val(getRecipient());
        if($this.attr('file') != 'no'){
            jq.post("components/com_frmessage/public/uploadlib/ajaxDelFile.php",{ name : $this.attr('rel') })
            .done(function(){});
        }
    }
    
    // kiem tra trang thai disable button upload
    checkDisableButton();

    return false;
});

function checkDisableButton(){
    if($('#content-upload tr').length > 0){
        $('#btn_upload').attr('disabled', 'disabled');
        $('#attachment input.att').attr('disabled', 'disabled');
    }else{
        $('#btn_upload').removeAttr('disabled');
        $('#attachment input.att').removeAttr('disabled');
    }
}

function bytesToSize(bytes, precision){
    var kilobyte = 1024;
    var megabyte = kilobyte * 1024;
    var gigabyte = megabyte * 1024;
    var terabyte = gigabyte * 1024;

    if ((bytes >= 0) && (bytes < kilobyte)) {
        return bytes + ' B';

    } else if ((bytes >= kilobyte) && (bytes < megabyte)) {
        return (bytes / kilobyte).toFixed(precision) + ' KB';

    } else if ((bytes >= megabyte) && (bytes < gigabyte)) {
        return (bytes / megabyte).toFixed(precision) + ' MB';

    } else if ((bytes >= gigabyte) && (bytes < terabyte)) {
        return (bytes / gigabyte).toFixed(precision) + ' GB';

    } else if (bytes >= terabyte) {
        return (bytes / terabyte).toFixed(precision) + ' TB';

    } else {
        return bytes + ' B';
    }
}

// popup select recipient
jq('#btn_select_recipient').bind('click', function(e) {
    e.preventDefault();
    jq.Zebra_Dialog('', {
        source:  {'ajax': 'index.php?option=com_popup&view=bg&task=popup_frmessage&tmpl=component'},
        height: 750,
        width: 850,
        title:  '配信先選択',
        type:  '',
        buttons:  [
            {caption: 'キャンセル', callback: function() {}},
            {caption: 'OK', callback: function() {
                jq('#OCBA_CODE_LIST').val(jq('#OCBA_CODE_LIST').val()+jq('#list_uid').val());
                //alert(jq('#OCBA_CODE_LIST').val());
                jq('#OCBA_CODE_LIST_SHOW').val(jq('#OCBA_CODE_LIST').val().replace(/\([^)]+\)/g,''));
            }

            }
        ]
    });
});
</script>